#! /usr/bin/env python

# $XORP: other/testbed/tools/xttable.py,v 1.8 2001/11/02 23:57:44 atanu Exp $

import string
import getopt, sys
from time import *

from xtxml import Xorp

"""
Generate a HTML table that show the connections of the PCs to the switch
"""

def main():
	def usage():
		print "usage: %s [-h|--help] [-d|--debug] -p|--physical xorp.xml" \
		      %  sys.argv[0]
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hp:d", \
				   ["help", "physical=", "debug"])
	except getopt.GetoptError:
		usage()
		sys.exit(1)
	physical = ""
	debug = 0
	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit()
		if o in ("-p", "--physical"):
			physical = a
		if o in ("-d", "--debug"):
			debug = 1

	if "" == physical:
		usage()
		sys.exit(1)

	x = Xorp(physical, debug).start()

	start = "<th>"
	end = "</th>"
	newline = "<br>"

	z = {}

	for i in x:
		z[i[2]] = i

	l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
	l.reverse();

	ran = map(repr, range(1,9))
	rans = map(repr, range(1,9))
	rans.insert(0, ' ')

	print '<table border="5">'

	print "<tr>",
	for i in rans:
		print start,
		print i,
		print end
	print "</tr>"

# xorp0 00:02:b3:10:e2:ec e1 33 fxp 319 f0 yes
# xorp0 00:02:b3:10:df:aa d1 25 fxp 320 f1 yes
# xorp0 00:80:c8:b9:3f:60 c1 17 tulip 321 t0 yes
# xorp0 00:80:c8:b9:3f:5f b1 9 tulip 330 t1 yes
# xorp0 00:80:c8:b9:3f:5d e7 39 tulip 327 t2 yes
# xorp0 00:80:c8:b9:3f:5e e8 40 tulip 332 t3 yes
# xorp0 00:02:B3:10:DF:4F a1 1 fxp 237 main no
# xorp1 00:02:b3:10:e2:e9 e2 34 fxp 312 unknown yes
# xorp1 00:02:b3:10:e3:e7 d2 26 fxp 316 unknown yes
# xorp1 00:80:c8:b9:05:5b b2 10 tulip 318 unknown yes
# xorp1 00:80:c8:b9:05:5c c2 18 tulip 317 unknown yes
# xorp2 00:02:b3:10:dc:df e3 35 fxp 323 unknown yes
# xorp2 00:02:b3:09:6f:80 d3 27 fxp 324 unknown yes
# xorp2 00:80:c8:b9:17:c3 b3 11 tulip 326 unknown yes
# xorp2 00:80:c8:b9:17:c4 c3 19 tulip 325 unknown yes
# xorp3 00:02:b3:10:e2:eb e4 36 fxp 308 unknown yes
# xorp3 00:02:b3:10:e2:ed d4 28 fxp 309 unknown yes
# xorp3 00:80:c8:cf:ac:54 c4 20 tulip 310 unknown yes
# xorp3 00:80:c8:cf:ac:53 b4 12 tulip 315 unknown yes
# xorp4 00:02:b3:10:dc:a5 e5 37 fxp 304 unknown yes
# xorp4 00:02:b3:10:c3:9c d5 29 fxp 306 unknown yes
# xorp4 00:80:c8:b9:43:50 c5 21 tulip 311 unknown yes
# xorp4 00:80:c8:b9:43:4f b5 13 tulip 314 unknown yes

	for i in l:
		print "<tr>",
		print start
		print i,
		print end
		for j in  ran:
			print start
			try:
				line = z[i+j]
				print line[0], newline, \
				      line[1], newline, \
				      line[6], line[4], \
				      line[5],
			except:
				print "-"
			print end
		print "</tr>"

	print '</table>'

	print "Generated by %s" % sys.argv[0]
	print strftime("%a, %d %b %Y %H:%M:%S %Z", localtime(time()))

	sys.exit()

if __name__ == '__main__':
	main()
	
